package com.admin.education.controllers.api.v1;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.categorys.CategoryTemplateTransfer;
import com.admin.education.dataTransferObject.categorys.CategorysTransfer;
import com.admin.education.dataTransferObject.votes.VotesTransfer;
import com.admin.education.services.Interfaces.ICategoryService;
import com.admin.education.services.Interfaces.IVotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.Callable;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/v1/categorys")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IVotesService votesService;
    @GetMapping("")
    public Callable<IResponseMessage> getCategorys(){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<List<CategorysTransfer>> result = categoryService.getCategorys();
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<List<CategorysTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}/templates")
    public Callable<IResponseMessage> GetTemplates(@PathVariable Long id){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<List<CategoryTemplateTransfer>> result = categoryService.getTemplates(id);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<List<CategoryTemplateTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}/votes")
    public Callable<IResponseMessage> getVotes(@PathVariable("id") Long id){
        return ()->{
            IResponseMessage responseMessage =null;

            ServicesExcuteResult<List<VotesTransfer>> result = this.votesService.queryVotes(id);
            if(result.isSuccess()){
                responseMessage = new ResponseMessageWithData<>(Result.Success,result.getData());
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
}
